package leetcode

//https://leetcode.com/problems/letter-combinations-of-a-phone-number/
fun main(args: Array<String>) {

    println(letterCombinations("23"))
}

val letterMap = arrayOf(
        "0", "1",
        arrayOf('a', 'b', 'c'),
        arrayOf('d', 'e', 'f'),
        arrayOf('g', 'h', 'i'),
        arrayOf('j', 'k', 'l'),
        arrayOf('m', 'n', 'o'),
        arrayOf('p', 'q', 'r', 's'),
        arrayOf('t', 'u', 'v'),
        arrayOf('w', 'x', 'y', 'z')

)


fun letterCombinations(digits: String): List<String> {

    val result = ArrayList<String>()

    if (digits.isEmpty())return result


    letterCombinationsRecursive(digits, 0, "", result)
    return result

}

fun letterCombinationsRecursive(digits: String, level: Int, str: String, result: ArrayList<String>) {

    if (level == digits.length) {
        result.add(str)
        return
    }
    val letters = letterMap[digits[level] - '0'] as Array<Char>
    for (letter in letters) {
        letterCombinationsRecursive(digits, level + 1, str.plus(letter), result)
    }
}
